package com.qxzn.app.personalcenter.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.qxzn.app.bean.ProvinceBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.view.CustomItemView;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.AppUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增收货地址、编辑收货地址
 */
public class AddShippingAddressActivity extends BaseActivity {

    @BindView(R2.id.ll_root)
    LinearLayout llRoot;
    @BindView(R2.id.civ_name)
    CustomItemView civName;
    @BindView(R2.id.civ_phone)
    CustomItemView civPhone;
    @BindView(R2.id.civ_area)
    CustomItemView civArea;
    @BindView(R2.id.civ_address)
    CustomItemView civAddress;
    @BindView(R2.id.tv_save)
    TextView tvSave;
    private String title;
    public static final String addShippingAddress = "新增收货地址";
    public static final String editShippingAddress = "编辑收货地址";
    private OptionsPickerView<String> cityOptions;
    private int provincePos;
    private int cityPos;
    private int areaPos;
    private List<String> provinceList = new ArrayList<>();
    private List<List<String>> cityList = new ArrayList<>();
    private List<List<List<String>>> areaList = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.activity_add_shipping_address;
    }

    @Override
    protected void initView() {
//        llRoot.getViewTreeObserver().addOnGlobalLayoutListener(() -> setStatusBar());
    }

    @Override
    protected void iniTitleBar() {
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        title = getIntent().getStringExtra("title");
        tv_title.setText(title);
        if (editShippingAddress.equals(title)) {
            tvSave.setText("修改");
            TextView textView = new TextView(this);
            textView.setId(R.id.tv_delete);
            textView.setText("删除");
            textView.setTextColor(getResources().getColor(R.color.white));
            textView.setOnClickListener(this);
            ll_right_bar.addView(textView);
        } else {
            tvSave.setText("保存");
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.tv_delete) {
            AppUtil.showLog("删除收货地址");
        }
    }

    private void showCityPicker() {
        if (cityOptions == null) {
            cityOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    provincePos = options1;
                    cityPos = option2;
                    areaPos = options3;
                    civArea.setRightText(provinceList.get(options1) + "、" + cityList.get(options1).get(option2) + "、" + areaList.get(options1).get(option2).get(options3));
                    AppUtil.showLog(cityList.get(options1).get(option2));
                }
            }).setTitleText("请选择城市").setSubmitColor(getResources().getColor(R.color.white)).setCancelColor(getResources().getColor(R.color.main_blue))
                    .setTitleBgColor(getResources().getColor(R.color.main_blue)).setTitleColor(getResources().getColor(R.color.white)).build();
            List<ProvinceBean> provinceBeanList = JSONArray.parseArray(AppUtil.getAssetsText(this, "citys.json"), ProvinceBean.class);
            List<ProvinceBean.CityBean> city;
            for (ProvinceBean provinceBean : provinceBeanList) {//遍历省份
                List<String> cityList = new ArrayList<>();//该省的城市列表（第二级）
                List<List<String>> areaList = new ArrayList<>();//该省的所有地区列表（第三极）
                city = provinceBean.getCity();
                for (ProvinceBean.CityBean cityBean : city) {//遍历该省份的所有城市
                    cityList.add(cityBean.getName());//添加城市
                    areaList.add(cityBean.getArea());//添加该所有地区数据
                }
                this.provinceList.add(provinceBean.getName());
                this.cityList.add(cityList);
                this.areaList.add(areaList);
            }
            cityOptions.setPicker(provinceList, cityList, areaList);
        }
        cityOptions.show();
    }

    @OnClick({R2.id.civ_area, R2.id.tv_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.civ_area) {
            showCityPicker();
        } else if (i == R.id.tv_save) {
            if (editShippingAddress.equals(title)) {

            } else if (addShippingAddress.equals(title)) {

            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // 获得当前得到焦点的View，一般情况下就是EditText（特殊情况就是轨迹求或者实体案件会移动焦点）
            View v = getCurrentFocus();
            if (AppUtil.isShouldHideInput(v, ev)) {
                AppUtil.hideSoftInput(this, v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

}
